package com.app.OOS.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*import java.util.UUID;*/
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.app.OOS.domain.Batch;
import com.app.OOS.domain.Batch_courseware;
import com.app.OOS.domain.Course;
import com.app.OOS.domain.Course_Courseware;
import com.app.OOS.domain.Courseware;
import com.app.OOS.domain.Record;
import com.app.OOS.domain.Score;
import com.app.OOS.domain.User;
import com.app.OOS.domain.User_batch;
import com.app.OOS.domain.User_courseware_batch;
import com.app.OOS.service.AdminService;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;

@Controller
public class AdminController {
	@Autowired
	AdminService adminService;
	//审核学员
	@ResponseBody
	@RequestMapping("/selectUsers")
	public  Map<String, Object> selectUsers(@RequestParam("page")Integer page,@RequestParam("limit")int limit) {
		int	tmp=(page-1)*limit;
		Map<String, Object> map = new HashMap<String, Object>();
		List<User> list = adminService.selectUsers(tmp,limit);
		int tol = adminService.tol();
		map.put("status", 200);
		map.put("tol", tol);
		map.put("list", list);
		map.put("message", "学员信息");
		return map;
	}
	
	//通过学员
	@ResponseBody
	@RequestMapping("/updateUsers")
	public  Map<String, Object> updateUsers(@RequestParam("userId")int userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		adminService.updateUsers(userId);
		map.put("message", "学员信息");
		return map;
	}
	//不通过学员
	@ResponseBody
	@RequestMapping("/deleteUsers")
	public  Map<String, Object> deleteUsers(@RequestParam("userId")int userId) {
		Map<String, Object> map = new HashMap<String, Object>();
		adminService.deleteUsers(userId);
		map.put("message", "学员信息");
		return map;
	}
	
	//课程查看
	@ResponseBody
	@RequestMapping("/selectCourse")
	public  Map<String, Object> selectCourse(@RequestParam("page")Integer page,@RequestParam("limit")int limit) {
		int	tmp=(page-1)*limit;
		Map<String, Object> map = new HashMap<String, Object>();
		List<Course> list = adminService.selectCourse(tmp,limit);
		int tol = adminService.tol2();
		map.put("status", 200);
		map.put("tol", tol);
		map.put("list", list);
		map.put("message", "课程信息");
		return map;
	}
	
	
	@ResponseBody
	@RequestMapping("/selectCourses")
	public  List<Course> selectCourses() {
		List<Course> list = adminService.selectCourses();
		return list;
	}

	//删除课程
		@ResponseBody
		@RequestMapping("/deleteCourse")
		public  Map<String, Object> deleteCourse(@RequestParam("courseId")int courseId) {
			Map<String, Object> map = new HashMap<String, Object>();
			adminService.deleteCourse(courseId);
			map.put("message", "删除信息");
			return map;
		}
	//更新课程
		@ResponseBody
		@RequestMapping("/updateCourse")
		public  Map<String, Object> updateCourse(@RequestParam("courseName")String courseName,@RequestParam("courseId")int courseId) {
			Map<String, Object> map = new HashMap<String, Object>();
			String msg= adminService.updateCourse(courseName, courseId);
			map.put("message", msg);
			return map;
		}
		
	//新增课程
		@ResponseBody
		@RequestMapping("/insertCourse")
		public  Map<String, Object> insertCourse(@RequestParam("courseId")int courseId,@RequestParam("courseName")String courseName) {
			Map<String, Object> map = new HashMap<String, Object>();
			String msg= adminService.insertCourse(courseId, courseName);
			map.put("message", msg);
			return map;
		}
		
	//批次查询
		@ResponseBody
		@RequestMapping("/selectBatch")
		public  Map<String, Object> selectBatch(@RequestParam("page")Integer page,@RequestParam("limit")int limit) {
			int	tmp=(page-1)*limit;
			Map<String, Object> map = new HashMap<String, Object>();
			List<Batch> list = adminService.selectBatch(tmp,limit);
			int tol = adminService.tol3();
			map.put("status", 200);
			map.put("tol", tol);
			map.put("list", list);
			map.put("message", "学员信息");
			return map;
		}	
		
		@ResponseBody
		@RequestMapping("/selectBatchs")
		public  List<Batch> selectBatchs() {
			List<Batch> list = adminService.selectBatchs();
			return list;
		}
	
	//删除批次
		@ResponseBody
		@RequestMapping("/deleteBatch")
		public  Map<String, Object> deleteBatch(@RequestParam("batchId")int batchId) {
			Map<String, Object> map = new HashMap<String, Object>();
			adminService.deleteBatch(batchId);
			map.put("message", "批次信息");
			return map;
		}
	//更新课程
		@ResponseBody
		@RequestMapping("/updateBatch")
		public  Map<String, Object> updateBatch(@RequestParam("batchName")String batchName,@RequestParam("batchId")int batchId) {
			Map<String, Object> map = new HashMap<String, Object>();
			String msg= adminService.updateBatch(batchName, batchId);
			map.put("message", msg);
			return map;
		}	
	//新增课程
		@ResponseBody
		@RequestMapping("/insertBatch")
		public  Map<String, Object> insertBatch(@RequestParam("batchId")int batchId,@RequestParam("batchName")String batchName) {
			Map<String, Object> map = new HashMap<String, Object>();
			String msg= adminService.insertBatch(batchId, batchName);
			map.put("message", msg);
			return map;
		}
	//查询课件
		@ResponseBody
		@RequestMapping("/selectCourseware")
		public  Map<String, Object> selectCourseWare(@RequestParam("page")Integer page,@RequestParam("limit")int limit) {
		int	tmp=(page-1)*limit;
			Map<String, Object> map = new HashMap<String, Object>();
			List<Course_Courseware> list = adminService.selectCourseWare(tmp,limit);
			int tol = adminService.tol4();
			map.put("status", 200);
			map.put("tol", tol);
			map.put("list", list);
			map.put("message", "学员信息");
			return map;
		}
		
		@ResponseBody
		@RequestMapping("/selectCourseWares")
		public  List<Courseware> selectCourseWares() {
			List<Courseware> list = adminService.selectCourseWares();
			return list;
		}
	//删除课件
		@ResponseBody
		@RequestMapping("/deleteCourseWare")
		public  Map<String, Object> deleteCourseWare(@RequestParam("coursewareId")int coursewareId) {
			Map<String, Object> map = new HashMap<String, Object>();
			adminService.deleteCourseWare(coursewareId);
			map.put("message", "批次信息");
			return map;
		}
		
	//更新课件
		@ResponseBody
		@RequestMapping("/updateCourseWare")
		public  Map<String, Object> updateCourseWare(@RequestParam("coursewareName")String coursewareName,@RequestParam("courseId")int courseId,@RequestParam("coursewareId")int coursewareId) {
			Map<String, Object> map = new HashMap<String, Object>();
			adminService.updateCourseWare(coursewareName, courseId,coursewareId);
			map.put("message", "修改课件信息");
			return map;
		}
	
	/*//新增课件
		@ResponseBody
		@RequestMapping("/insertCourseWare")
		public  Map<String, Object> insertCourseWare(@RequestParam("coursewareId")int coursewareId,@RequestParam("coursewareName")String coursewareName,@RequestParam("courseId")int courseId,@RequestParam("src")String src,@RequestParam("Ctime")int Ctime) {
			Map<String, Object> map = new HashMap<String, Object>();
			String msg=adminService.insertCourseWare(coursewareId, coursewareName, courseId,src,Ctime);
			map.put("message", msg);
			return map;
		}*/
		
		
	//查询 批次-课程
		@ResponseBody
		@RequestMapping("/selectBatchCourseWare")
		public  Map<String, Object> selectBatchCourseWare(@RequestParam("page")Integer page,@RequestParam("limit")int limit) {
			int	tmp=(page-1)*limit;
			Map<String, Object> map = new HashMap<String, Object>();
			List<Batch_courseware> list = adminService.selectBatchCourseWare(tmp,limit);
			int tol = adminService.tol5();
			map.put("status", 200);
			map.put("tol", tol);
			map.put("list", list);
			map.put("message", "批次-课程信息");
			return map;
		}
	//删除批次-课件
		@ResponseBody
		@RequestMapping("/deleteBatchCourseWare")
		public  Map<String, Object> deleteBatchCourseWare(@RequestParam("batch_courseware_id")int batch_courseware_id) {
			Map<String, Object> map = new HashMap<String, Object>();
			adminService.deleteBatchCourseWare(batch_courseware_id);
			map.put("message", "删除信息");
			return map;
		}
	//更新课件
		@ResponseBody
		@RequestMapping("/updateBatchCourseWare")
		public  Map<String, Object> updateBatchCourseWare(@RequestParam("batchId")int batchId,@RequestParam("coursewareId")int coursewareId,@RequestParam("batch_courseware_id")int batch_courseware_id) {
			Map<String, Object> map = new HashMap<String, Object>();
			String msg=adminService.updateBatchCourseWare(batchId, coursewareId, batch_courseware_id);
			map.put("message",msg);
			return map;
		}
		
	/*//更新课件
		@ResponseBody
		@RequestMapping("/insertBatchCourseWare")
		public  Map<String, Object> insertBatchCourseWare(@RequestParam("batchId")int batchId,@RequestParam("coursewareId")int coursewareId,@RequestParam("batch_courseware_id")int batch_courseware_id) {
			Map<String, Object> map = new HashMap<String, Object>();
			String msg=adminService.insertBatchCourseWare(batchId, coursewareId, batch_courseware_id);
			map.put("message",msg);
			return map;
		}*/
		
	//查询学员-批次
		@ResponseBody
		@RequestMapping("/selectUserBatch")
		public  Map<String, Object> selectUserBatch(@RequestParam("page")Integer page,@RequestParam("limit")int limit) {
			int	tmp=(page-1)*limit;
			Map<String, Object> map = new HashMap<String, Object>();
			List<User_batch> list = adminService.selectUserBatch(tmp,limit);
			int tol = adminService.tol6();
			map.put("status", 200);
			map.put("tol", tol);
			map.put("list", list);
			map.put("message", "学员-批次信息");
			return map;
		}	
		
	//删除 学员-批次
		@ResponseBody
		@RequestMapping("/deleteUserBatch")
		public  Map<String, Object> deleteUserBatch(@RequestParam("user_batch_id")int user_batch_id) {
			Map<String, Object> map = new HashMap<String, Object>();
			adminService.deleteUserBatch(user_batch_id);
			map.put("message", "删除信息");
			return map;
		}	
		
	//查询所有学员 不带参数
		@ResponseBody
		@RequestMapping("/selectUseres")
		public  List<User> selectUsers() {
			List<User> list = adminService.selectUseres();
			return list;
		}
		
	//查询所有学员 不带参数
		@ResponseBody
		@RequestMapping("/updateUserBatch")
		public  Map<String, Object> updateUserBatch(@RequestParam("user_batch_id")int user_batch_id,@RequestParam("batchId")int batchId) {
			Map<String, Object> map = new HashMap<String, Object>();
			adminService.updateUserBatch(user_batch_id,batchId);
			map.put("message","修改信息");
			return map;
		}	
	
	//查询所有学员 不带参数
		@ResponseBody
		@RequestMapping("/insertUserBatch")
		public  Map<String, Object> insertUserBatch(@RequestParam("user_batch_id")int user_batch_id,@RequestParam("batchId")int batchId,@RequestParam("userId")int userId) {
			Map<String, Object> map = new HashMap<String, Object>();
			String msg =adminService.insertUserBatch(user_batch_id, userId, batchId);
			map.put("message",msg);
			return map;
		}
		
	//上传文件到服务器
		@RequestMapping("/upload_submit")
		@ResponseBody
		public Map<String, Object> upload_submit(@RequestParam("file")MultipartFile file,HttpServletRequest request,@RequestParam("coursewareId")int coursewareId,@RequestParam("coursewareName")String coursewareName,@RequestParam("courseId")int courseId,@RequestParam("cwPeriod")int cwPeriod) throws IllegalStateException, IOException{
			System.out.println("file:"+file);
			System.out.println("coursewareId:"+coursewareId);
			System.out.println("coursewareName:"+coursewareName);
			System.out.println("cwPeriod:"+cwPeriod);
			System.out.println("courseId:"+courseId);
			//设置上传服务器的路径 要保证有loads这个目录
	        String path = "http://122.51.31.23:8080/uploads/";
	        System.out.println(file);
	        //获取上传文件的名称
	        String filename = file.getOriginalFilename();
	        System.out.println("http:122.51.31.23:8080/uploads/"+filename);
	        //把文件名设置成一个唯一值
	     /* String uid = UUID.randomUUID().toString().replace("-","");
	 		
	        filename = uid + "_" + filename;*/
	        //创建客户端对象
	        Client client = Client.create();
	        //与服务器进行连接
	        WebResource webResource = client.resource(path + filename);
	        //进行文件上传，并将文件转换成字节流
	        webResource.put(file.getBytes());
	        String src=path + filename;
	        adminService.insertCourseWare(coursewareId, coursewareName, courseId, cwPeriod,src);
	        Map<String, Object> map = new HashMap<>();
	        map.put("coursewareId", coursewareId);
	        map.put("courseId", courseId);
	        return map;
		}
		
	//查看学生成绩
		@RequestMapping("/selectScore")
		@ResponseBody
		public  Map<String, Object> selectScore(@RequestParam("page")Integer page,@RequestParam("limit")int limit) {
			int	tmp=(page-1)*limit;
			Map<String, Object> map = new HashMap<String, Object>();
			List<Score> list = adminService.selectScore(tmp,limit);
			int tol = adminService.tol7();
			map.put("status", 200);
			map.put("tol", tol);
			map.put("list", list);
			map.put("message", "学员成绩信息");
			return map;
		}
		
		//查看学生成绩
				@RequestMapping("/updateCtime")
				@ResponseBody
				public  Map<String, Object> updateCtime(@RequestParam("Ctime")double Ctime,@RequestParam("coursewareId")int coursewareId) {
					int c=(int)(Ctime);
					adminService.updateCtime(c, coursewareId);
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("message", "学员成绩信息");
					return map;
				}
		
		
				@RequestMapping("/selectRR")
				@ResponseBody
				public  Map<String, Object> selectRR() {
					
					List<User_courseware_batch> list =adminService.seclctRR();
					Map<String, Object> map = new HashMap<String, Object>();
					
					map.put("status", 200);
					map.put("tol", 1);
					map.put("list", list);
					map.put("message", "学员成绩信息");
					return map;
				}
		
		
}
